<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include/src :: form_base_js_and_css"/>
</head>
<body style="background-color: #f2f2f2; height: 100%;">

<div id="id_panel">
</div>
<script th:inline="javascript">
	var dto = $.parseJSON(JSON.stringify([[${dto}]]));

	// 表单初始化
	var formObj = null;
	//使用layui的sku模块
	layui.config({
		//自定义layui组件的目录,设定扩展的layui模块的所在目录，一般用于外部模块扩展
		base: '/static/js/layui_extends/',
		version: true
	}).use(['form', 'jquery', 'laydate', 'YsForm', 'YsModal'], function(){
		var $ = layui.jquery,
				form = layui.form,
				laydate = layui.laydate,
				YsForm = layui.YsForm,
				YsModal = layui.YsModal;
		/*
		* 表单初始化
		* 参数1：表单配置参数
		* 参数2：子表配置参数
		*/
		formObj = YsForm.init(
				{
					id:'id_panel', // 页面容器ID
					dto: dto
				}
		);


		/*
		 * 监听币种输入框
		 */
		var domForm = $('#' + window.formObj.c.id_form);
		domForm.find('input[name="disassembleNum"]').on('change',function (e) {
			calFormItem();
		});


		formObj.matchValuesAfterModalSelectRadio=function (colCode, rowSelected, extraData) { // 单选框选中数据后，匹配表单数值
			var t = formObj;
			var domForm = $('#' + t.c.id_form);
			for(var i = 0; i < t.c.dto.rows.length; i++){
				var col = t.c.dto.rows[i];
				if (col.code == colCode) {
					// 给选择框设置的字段赋值
					domForm.find('input[name="' + colCode + '"]').val(rowSelected[col.modalValueCode]);
				} else if(col.modalRelativeCode == colCode) {
					domForm.find('input[name="' + col.code + '"]').val(rowSelected[col.modalValueCode]);
				}
			}
			getSonList(colCode);
		};



	});


</script>

<script>




	function getSonList(colCode) {
		console.log('---------colCode---------',colCode);
		if(colCode == 'productName'){
			var domForm = $('#' + window.formObj.c.id_form);
			//库存id
			var stockControlId = domForm.find('input[name="stockControlId"]').val();
			//商品编码
			var productCode = domForm.find('input[name="productCode"]').val();
			//拆装数量
			var disassembleNum = domForm.find('input[name="disassembleNum"]').val();

			var sonlist = formObj.c.subGridObj.c.sonlist;
			if( (sonlist==null || sonlist.length<=0) ||(sonlist!=null && sonlist.length>0 && productCode!=sonlist[0].productCode)){
				sonlist = [];
				$.ajax({
					url: '/api/inter/tcgOfflinePurchaseInStorage/searchByPcodeSearch',
					data: {
						'_ys_pCode': productCode, //商品编码
						'stockControlId': stockControlId,
					},
					type: 'POST',
					async: true,
					success: function (result) {
						console.log('保存返回result:',result);
						if(result.code=='0'){
							for(var i=0;i<result.data.length;i++){
								var row = result.data[i];
								sonlist.push({
									productCode:row._ys_pCode,
									subCode:row.code,
									subName:row.name,
									subBomNum:row.num,
									subDisNum:disassembleNum?(parseFloat(disassembleNum) * parseFloat(row.num)).toFixed(0):0
								})
							}
							formObj.c.subGridObj.c.sonlist = sonlist;
							formObj.c.subGridObj.reloadGrid();

						}else{//失败
							layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
						}
					},
					error: function () {
						layer.close(layindex);
						layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
					}
				});
			}


		}


	}

	

	/*
	 * 自动计算表单数据
	 */
	function calFormItem() {

		var domForm = $('#' + window.formObj.c.id_form);
		//拆装数量
		var disassembleNum = domForm.find('input[name="disassembleNum"]').val();
		var chwNum = domForm.find('input[name="chwNum"]').val();
		if(disassembleNum && chwNum){
			if(disassembleNum>chwNum){
				layer.msg("拆装数量 应该小于等于 库存数量!");
				disassembleNum = chwNum;
				domForm.find('input[name="disassembleNum"]').val(chwNum);
			}
		}
		var sonlist = formObj.c.subGridObj.c.sonlist;
		for (var i = 0; i < sonlist.length; i++) {
			formObj.c.subGridObj.c.sonlist[i]['subDisNum'] = disassembleNum?(parseFloat(disassembleNum) * parseFloat(formObj.c.subGridObj.c.sonlist[i]['subBomNum'])).toFixed(0):0
		}
		formObj.c.subGridObj.reloadGrid();

	}


	function opeTplScript(subCode){
		console.log('---------subCode---------',subCode);

		var sonlist = formObj.c.subGridObj.c.sonlist;
		//console.log('---------sonlist---------',sonlist);
		var isSame = false;
		for (var i = 0; i < sonlist.length; i++) {
			if(sonlist[i].subCode == subCode){
				isSame = true;
			}
			if(isSame && sonlist[i].subCode != subCode ){
				var row = sonlist[i-1];
				var addRow ={
					productCode:row.productCode,
					subCode:row.subCode,
					subName:row.subName,
					subBomNum:row.subBomNum,
					subDisNum:row.subDisNum,
				};
				formObj.c.subGridObj.c.sonlist.splice(i, 0, addRow);
				isSame = false;
			}

		}
		if(isSame){
			var row = sonlist[sonlist.length-1];
			var addRow ={
				productCode:row.productCode,
				subCode:row.subCode,
				subName:row.subName,
				subBomNum:row.subBomNum,
				subDisNum:row.subDisNum,
			};
			formObj.c.subGridObj.c.sonlist.push(addRow);
			isSame = false;
		}
		formObj.c.subGridObj.reloadGrid();
	}




</script>


</body>
</html>